package com.lovo.project.servlet.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.project.enums.OrderCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.OrderDO;
import com.lovo.project.service.OrderService;
import com.lovo.project.service.impl.OrderServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "FindByItemServlet",value = "/order/findByItem")
public class FindByItemServlet extends HttpServlet {
    private OrderService service = new OrderServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            String type = request.getParameter("type");
            String value = request.getParameter("value");

            String page = request.getParameter("page");
            String size = request.getParameter("size");

            PageHelper.startPage(page == null ? 1 : Integer.parseInt(page),
                    size == null ? 5 : Integer.parseInt(size));

            List<OrderDO> list = service.findByItem(type, value);

            PageInfo pageInfo =PageInfo.of(list);

            response.getWriter().print(ResultVO.success().add(pageInfo).toJson());
        }catch (OurException oe){
            response.getWriter().print(ResultVO.fail(oe.getCodeEnum()).toJson());
        }catch (Exception e){
            response.getWriter().print(ResultVO.fail(OrderCodeEnum.ORDER_UNKNOW_ENUM));
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }
}
